from fastapi import APIRouter, Depends, Request
from sqlalchemy.orm.session import Session

from server.controller.menu import (
    menu_add, get_menu_tree, menu_update, menu_delete
)
from server.dependencies.check_permission import check_permission
from server.etc.database import create_db
from server.schemas.comment import SystemErrors, WriteResponse
from server.schemas.menu import (
    AddMenuSchema, UpdateMenuSchema, DeleteMenuSchema,
    MenuAlreadyExists, MenuResponseList
)

router = APIRouter()


@router.post(
    "/addmenu",
    response_model=WriteResponse,
    responses={
        400: {"model": MenuAlreadyExists, "description": "菜单已存在"},
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def add_menu(
        request: Request,
        createData: AddMenuSchema,
        db: Session = Depends(create_db),
        current_user: str = Depends(check_permission("menu:manage"))
):
    """
    添加菜单
    """
    result = menu_add(request, createData, db, current_user)
    return result


@router.get(
    "/getmenutree",
    response_model=MenuResponseList,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def get_menu(
        request: Request,
        db: Session = Depends(create_db),
        current_user: str = Depends(check_permission("menu:manage"))
):
    """
    获取菜单树
    """
    result = get_menu_tree(request, db, current_user)
    return result


@router.post(
    "/updatemenu",
    response_model=WriteResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def update_menu(
        request: Request,
        updateDate: UpdateMenuSchema,
        db: Session = Depends(create_db),
        current_user: str = Depends(check_permission("menu:manage"))
):
    """
    更新菜单
    """
    result = menu_update(request, updateDate, db, current_user)
    return result


@router.post(
    "/deletemenu",
    response_model=WriteResponse,
    responses={
        500: {"model": SystemErrors, "description": "系统错误"},
    }
)
async def delete_menu(
        request: Request,
        deleteData: DeleteMenuSchema ,
        db: Session = Depends(create_db),
        current_user: str = Depends(check_permission("menu:manage"))
):
    """
    删除菜单
    """
    result = menu_delete(request, deleteData, db, current_user)
    return result

